package com.ruoyi.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.app.annotation.AppSecurity;
import com.ruoyi.app.util.ReqMsg;
import com.ruoyi.app.util.RspMsg;
import com.ruoyi.bizsys.service.IAppUserService;
import com.ruoyi.business.service.IAwardDistributionService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.Func;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 分销层级
 *
 * @author cyh
 * @date 2022/6/23 5:41 下午
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/invite")
public class DistributionController extends BaseController {

    private IAppUserService appUserService;

    private IAwardDistributionService awardDistributionService;

    @AppSecurity
    @PostMapping("/info")
    @Log(title = "获取用户邀请信息", businessType = BusinessType.GET)
    public RspMsg getInviteInfo(@RequestBody @Validated ReqMsg reqMsg) {
        return RspMsg.ok(appUserService.getInviteInfo(getUserId()));
    }

    @AppSecurity
    @PostMapping("/link/update")
    @Log(title = "更新用户邀请链接", businessType = BusinessType.UPDATE)
    public RspMsg updateInviteLink(@RequestBody @Validated ReqMsg reqMsg) {
        JSONObject jsonObject = JSON.parseObject(reqMsg.getData().toString());
        String inviteLink = jsonObject.getString("inviteLink");
        if (Func.isEmpty(inviteLink)) throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        return RspMsg.ok(appUserService.updateInviteLink(getUserId(), inviteLink));
    }



}
